package com.orange.core.util;

import cn.hutool.core.collection.CollUtil;
import com.orange.core.annotation.DictKey;
import com.orange.core.pojo.enums.IDict;
import org.reflections.Reflections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DictUtil {

    /**
     * 枚举字典包名
     */
    private static final String ENUM_DICT_PACKAGE_NAME = "com.orange.core.pojo.enums";
    private static final Map<String, List<Map<String, Object>>> DICT_MAP = new HashMap<>();

    public static Map<String, List<Map<String, Object>>> getDictMap() {
        if (CollUtil.isEmpty(DICT_MAP)) {
            synchronized (DictUtil.class) {
                if (CollUtil.isEmpty(DICT_MAP)) {
                    List<Class<? extends IDict>> dictClasses = scanDictEnum(ENUM_DICT_PACKAGE_NAME);
                    Map<String, List<Map<String, Object>>> dictMap = generateDict(dictClasses);
                    DICT_MAP.putAll(dictMap);
                }
            }
        }
        return DICT_MAP;
    }

    /**
     * 扫描指定包下的所有字典枚举
     */
    public static List<Class<? extends IDict>> scanDictEnum(String packageName) {
        return new Reflections(packageName).getSubTypesOf(IDict.class)
                .stream().filter(Class::isEnum).toList();
    }

    /**
     * 根据枚举字典类生成字典
     */
    public static Map<String, List<Map<String, Object>>> generateDict(List<Class<? extends IDict>> dictClasses) {
        Map<String, List<Map<String, Object>>> dictMap = new HashMap<>();
        for (Class<? extends IDict> dictClass : dictClasses) {
            List<Map<String, Object>> dictValues;
            DictKey dictKey = dictClass.getAnnotation(DictKey.class);
            if (dictKey != null) {
                dictValues = dictMap.computeIfAbsent(dictKey.value(), k -> new ArrayList<>());
            } else {
                String className = dictClass.getSimpleName();
                if (className.startsWith("E") && Character.isUpperCase(className.charAt(1))) {
                    className = className.substring(1);
                }
                if (className.endsWith("Enum")) {
                    className = className.substring(0, className.length() - 4);
                }
                StringBuilder sb = new StringBuilder().append(Character.toLowerCase(className.charAt(0)));
                for (int i = 1; i < className.length(); i++) {
                    char currentChar = className.charAt(i);
                    if (Character.isUpperCase(currentChar)) {
                        sb.append("_").append(Character.toLowerCase(currentChar));
                    } else {
                        sb.append(currentChar);
                    }
                }
                dictValues = dictMap.computeIfAbsent(sb.toString(), k -> new ArrayList<>());
            }
            IDict[] dictEnums = dictClass.getEnumConstants();
            for (IDict dict : dictEnums) {
                Map<String, Object> dictValueMap = new HashMap<>();
                dictValueMap.put("k", dict.getValue());
                dictValueMap.put("v", dict.getLabel());
                dictValues.add(dictValueMap);
            }
        }
        return dictMap;
    }
}
